#include "jsapi.h"
#include "js-engine.h"
#include "desktopClasses.h"
#include "mainview-parser.h"

static JSBool div_new(JSContext *cx, JSObject *obj, uintN argc, jsval *args, jsval *rval);

struct JSFunctionSpec div_methods[] = {
    {NULL, NULL}
};

JSClass divClass =
    { "div", JSCLASS_HAS_PRIVATE,
      JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
      JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
      JSCLASS_NO_OPTIONAL_MEMBERS
    };

struct property_parser div_parser[] = {
    /* from basicElement */
    {"cursor", set_property_string_value},
    {"dropTarget", set_property_boolean_value},
    {"enabled", set_property_boolean_value},
    {"height", set_property_integer_value},
    {"hitTest", set_property_string_value},
    {"mask", set_property_string_value},
    {"name", set_property_string_value},
    {"offsetHeight", set_property_integer_value},
    {"offsetWidth", set_property_integer_value},
    {"offsetX", set_property_integer_value},
    {"offsetY", set_property_integer_value},
    {"opacity", set_property_integer_value},
    {"pinX", set_property_integer_value},
    {"pinY", set_property_integer_value},
    {"rotation", set_property_float_value},
    {"tagName", set_property_string_value},
    {"tooltip", set_property_string_value},
    {"width", set_property_integer_value},
    {"visible", set_property_boolean_value},
    {"x", set_property_integer_value},
    {"y", set_property_integer_value},
		
    {"autoscroll", set_property_integer_value}, 
    {"background", set_property_string_value},
    {NULL, NULL},
};

static JSBool div_new(JSContext *cx, JSObject *obj, uintN argc, jsval *args, jsval *rval)
{
    return JS_TRUE;
}

JSObject *divClass_init(js_engine *engine, JSObject *parent)
{
    JSObject *proto = NULL;
    proto = JS_InitClass(engine->cx, engine->glob, parent,
			 &divClass, div_new,
			 0,NULL,NULL,NULL,NULL);
    g_return_val_if_fail(proto, FALSE);
    g_return_val_if_fail(JS_DefineFunctions(engine->cx, proto, div_methods), FALSE);
    return proto;
}

gboolean draw_div(GtkWidget *event_box, GdkEventExpose *event, gpointer ptr)
{
    jsval height, opacity, width, visible, x, y, background;

    desktop_element *widget = (desktop_element *)ptr;
    cairo_t *cr;
    cairo_surface_t *background_image;
    PangoColor color_val;

    gdouble x_pos = 0, y_pos = 0, width_val = 0, height_val = 0;
    gdouble opacity_ratio = 1;

    cr = gdk_cairo_create(event_box->window);
  
    JS_GetProperty(widget->engine->cx, widget->jsobj, "height", &height);
    JS_GetProperty(widget->engine->cx, widget->jsobj, "opacity", &opacity);
    JS_GetProperty(widget->engine->cx, widget->jsobj, "width", &width);
    JS_GetProperty(widget->engine->cx, widget->jsobj, "visible", &visible);
    JS_GetProperty(widget->engine->cx, widget->jsobj, "x", &x);
    JS_GetProperty(widget->engine->cx, widget->jsobj, "y", &y);
    JS_GetProperty(widget->engine->cx, widget->jsobj, "background", &background);

    if(JSVAL_IS_STRING(background)){
	gchar *src_path = trans_unix_path_new(JS_GetStringBytes(JSVAL_TO_STRING(background)));
	if(src_path[0] == '#'){
	    pango_color_parse(&color_val, JS_GetStringBytes(JSVAL_TO_STRING(background)));

	}else{
	    background_image = cairo_image_surface_create_from_png(src_path);
	}
	g_free(src_path);
    }
    if(JSVAL_IS_NUMBER(x))
	JS_ValueToNumber(widget->engine->cx, x, &x_pos);
    if(JSVAL_IS_NUMBER(y))
	JS_ValueToNumber(widget->engine->cx, y, &y_pos);
    if(JSVAL_IS_NUMBER(width))
	JS_ValueToNumber(widget->engine->cx, width, &width_val);
    if(JSVAL_IS_NUMBER(height))
	JS_ValueToNumber(widget->engine->cx, height, &height_val);

    if(JSVAL_IS_INT(opacity)){
	int value = JSVAL_TO_INT(opacity);
	opacity_ratio = (gdouble)value/255;
    }
    if(JSVAL_IS_BOOLEAN(visible)){
	if(!JSVAL_TO_BOOLEAN(visible))
	    opacity_ratio = 0;
    }
    cairo_rectangle(cr, x_pos, y_pos, width_val, height_val);
    cairo_set_source_rgb(cr, (gdouble)color_val.red/65536, (gdouble)color_val.green/65535, (gdouble)color_val.blue/65535);
    cairo_paint_with_alpha(cr, opacity_ratio);
    cairo_destroy(cr);
    return FALSE;
}

void desktop_element_set_view_div(js_engine *engine, desktop_element *widget)
{
    desktop_element *view = JS_GetPrivate(engine->cx, engine->view);
    g_return_if_fail(view);
    g_signal_connect_after(view->view, "expose-event", G_CALLBACK(draw_div), widget);
}
